package com.mathew.spring.appsp.conf;

import java.util.Date;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class MQSend {

  private static final Logger logger = LoggerFactory.getLogger(MQSend.class);

  @Autowired
  private AmqpTemplate amqpTemplate;

  public String send() {

    String context = new Date().toString();
    HashMap<String, String> map = new HashMap<>();
    map.put("pattern", "date");
    map.put("data", context);

    // 消息操作模板
    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    // 发送消息方式一
    JSONObject jsonObj = new JSONObject(map);
    Message message = new Message("hello".getBytes(), messageProperties);
    // amqpTemplate.convertAndSend("node_queue", "./node_queue", map);
    logger.info("node_queue");

    Message rMessage = amqpTemplate.sendAndReceive("node_queue", message);
    return rMessage.toString();
  }
}
